$(function() {

    // 在线充值类型切换
    $(".pay-tab li:first").addClass("active");
    $(".pay-tab li").on("click", function() {
        $(this).addClass("active").siblings().removeClass("active");
        var index = $(this).index();
        var thisPanel = $(".pay-panel").eq(index);
        thisPanel.show().siblings().hide();
        Cookies.set("tab-target", 'pay-ebank');
    });

    // 记录 - 在线充值类型切换
    var tabTarget = Cookies.get("tab-target");
    $(".pay-tab li").each(function() {
        if ($(this).data("tab") == tabTarget) {
            $(".pay-tab li").removeClass("active");
            $(this).trigger("click");
        }
    });

    // 充值金额选择与银行选择
    $(".label-cell label, .bank-list label").on("click", function() {
        $(this).addClass("active").siblings().removeClass("active");
    });

    // 记录 - 银行选择
    $(".bank-list label:first").addClass("active");
    $(".bank-list label").on("click", function() {
        $(this).addClass("active").siblings().removeClass("active");
        Cookies.set("ebank-selected", $(this).attr("bank"));
    });
    var bankSelected = Cookies.get("ebank-selected");
    $(".bank-list label").each(function() {
        if ($(this).attr("bank") == bankSelected) {
            $(".bank-list label").removeClass("active");
            $(this).addClass("active");
        }
    });

    // 给定的充值金额
    $(".rating label").on("click", function() {
        var amount = $(".input-amount-qrcode");
        var closest = amount.closest(".amounts");
        var val = parseInt($(this).text());
        closest.find(".custom").next(".form-tips").remove();
        closest.find(".form-small").removeClass("has-error");
        amount.attr("fee", val);
        amount.val(val);
        if ((typeof subid !== "undefined") && subid == "zzb") {
            eventOfClickMoney($(this), gamepoint_convert_zzb);
        } else {
            $(".label-option span").text(val);
            payInduce($(this));
            updateFee($(this));
            eventOfClickMoney($(this), gamepoint_convert1);
        }
    });

    // 记录 - 网银充值金额
    var amountEbank = $(".rating label");
    amountEbank.each(function() {
        var val = parseInt($(this).text());
        if (val == Cookies.get("amount-qrcode")) {
            $(this).trigger("click");
        } else if ((typeof buy_number !== "undefined") && val == buy_number) {
            $(this).trigger("click");
        }
    });

    // 记录 - 自定义输入其他金额
    var amount = $(".input-amount-qrcode");
    amount.on("blur", function() {
        var val = parseInt($.trim(this.value));
        var min = $(this).data("min");
        var max = $(this).data("max");
        if (val > max) {
            Cookies.set("amount-qrcode", max);
        } else if (val < min) {
            Cookies.set("amount-qrcode", min);
        } else {
            Cookies.set("amount-qrcode", val);
        }
    });
    var amountInit = Cookies.get("amount-qrcode");
    if (typeof buy_number !== "undefined") {
        amount.val(buy_number).keyup().blur();
        amount.attr("fee", buy_number);
        var val = parseInt($.trim(amount.val()));
        var label = amount.closest(".label-item").find("label");
        label.removeClass("active");
        label.each(function() {
            if (parseInt($(this).text()) == val) {
                $(this).addClass("active").siblings().removeClass("active");
            }
        });
    } else if (amountInit !== "undefined") {
        amount.val(amountInit).keyup().blur();
        amount.attr("fee", amountInit);
        var val = parseInt($.trim(amount.val()));
        var label = amount.closest(".label-item").find("label");
        label.removeClass("active");
        label.each(function() {
            if (parseInt($(this).text()) == val) {
                $(this).addClass("active").siblings().removeClass("active");
            }
        });
    }
    if (isNaN(amountInit)) {
        var labelInit = $(".rating label:first");
        Cookies.set("amount-qrcode", parseInt(labelInit.text()));
        labelInit.click();
    }

    // 充值渠道
    $(".channel-tab label:first").addClass("active");
    $(".alipay-tips").css("display", "block");
    var bankList = $(".bank-list");
    bankList.parent().data("channel-id", $(".channel-tab label:last").index() + 1);
    $(".channel-tab label").on("click", function() {
        var index = $(this).index() + 1;
        var channelId = bankList.parent().data("channel-id");
        $(this).addClass("active").siblings().removeClass("active");
        Cookies.set("channel-ebank", $(this).attr("id"));
        // 支付宝优惠提示
        var display = (index === 1) ? "block" : "none";
        $(".alipay-tips").css("display", display);
        // 银行类型
        if (index == channelId) {
            bankList.show();
            // f**k IE
            if (isIE(7)) {
                var nextLi = bankList.closest("li").next("li");
                nextLi.css({
                    position: "relative"
                });
                nextLi.next("li").css({
                    position: "relative"
                });
            }
        } else {
            bankList.hide();
        }
    });

    // 记录 - 网银充值渠道
    var channelEbank = Cookies.get("channel-ebank");
    $(".channel-tab label").each(function() {
        if ($(this).attr("id") == channelEbank) {
            $(".channel-tab label").removeClass("active");
            $(this).trigger("click");
        }
    });

    // 折扣提示
    var discount = $(".discount");
    var child = discount.find(".zticon");
    var tipsDiscount = discount.find(".tips");
    child.hover(function() {
        tipsDiscount.show();
        // f**k IE
        discount.closest("li").addClass("tips-show");
    }, function() {
        tipsDiscount.hide();
        // f**k IE
        discount.closest("li").removeClass("tips-show");
    });

    // 微信支付弹层
    var btnWechat = $("#wechat");
    var wechatPop = $("#pop-wechat");
    btnWechat.on("click", function() {
        //  wechatPop.show();
        // goCenter(wechatPop);
    });

    resizePop(wechatPop);

    // 云闪付扫码弹层
    var btnUnionPayQrc = $("#unionpayqrc");
    var unionPayQrcPop = $("#pop-unionpayqrc");
    btnUnionPayQrc.on("click", function() {
        //btnUnionPayQrc.show();
        //goCenter(unionPayQrcPop);
    });

    resizePop(unionPayQrcPop);

    // 网上银行付款弹层
    var btnEbank = $("#alipay");
    var ebankPop = $("#pop-ebank");
    btnEbank.on("click", function() {
        //  ebankPop.show();
        //  goCenter(ebankPop);
    });

    resizePop(ebankPop);

    // 常见问题列表
    var tips = $(".pay-tips dt");
    tips.tiplistToggle();

    // 其他充值方式切换
    var labelHd = $(".others-hd label");
    $(".others-hd label:first").addClass("active");
    labelHd.on("click", function() {
        var index = $(this).index();
        var labelBd = $(".others-bd ul").parent();
        $(this).addClass("active").siblings().removeClass("active");
        labelBd.eq(index).show().siblings().hide();
        // labelBd.eq(index).find("input[type=text]:first").trigger("focus");
        // labelBd.eq(index).find("input[type=text]:not(.nofocus):first").trigger("focus");
        Cookies.set("tab-mobile", $(this).data("mobile"));
    });

    // 记录 - 其他充值方式切换
    var tabMobile = Cookies.get("tab-mobile");
    labelHd.each(function() {
        if ($(this).data("mobile") == tabMobile) {
            labelHd.removeClass("active");
            $(this).trigger("click");
        }
    });

    // 充值渠道切换
    $(".others-tab label:first").addClass("active");
    $(".others-tab label").on("click", function() {
        $(this).addClass("active").siblings().removeClass("active");
        Cookies.set("channel-carrier", $(this).attr("carrier"));
    });
    // 记录 - 充值运营商
    var channelCarrier = Cookies.get("channel-carrier");
    $(".others-tab label").each(function() {
        if ($(this).attr("carrier") == channelCarrier) {
            $(".others-tab label").removeClass("active");
            $(this).trigger("click");
        }
    });

    // 手机充值金额切换
    var mobileLabels = $(".card-rating label, .sms-rating label");
    // var mobileLabels = $(".card-rating label, .sms-rating label .others-rating label");
    mobileLabels.each(function() {
        $(this).on("click", function() {
            var fee = count_fee[0].fee;
            var amountNum = parseInt($(this).text());
            var amounts = $(this).closest(".amounts");
            var points = amounts.find(".custom span");
            points.text(amountNum * fee);
            amounts.attr("fee", amountNum);
            $(this).addClass("active").siblings().removeClass("active");
        });
    });

    // 记录 - 手机充值卡金额
    $(".card-rating label:first").addClass("active");
    $(".card-rating label").closest(".amounts").attr("fee", parseInt($(".card-rating label:first").text()));
    $(".card-rating label").on("click", function() {
        var amount = parseInt($(this).text());
        Cookies.set("amount-card", amount);
    });
    var amountCard = Cookies.get("amount-card");
    $(".card-rating label").each(function() {
        if (parseInt($(this).text()) == amountCard) {
            $(".card-rating label").removeClass("active");
            $(this).trigger("click");
        }
    });

    // 记录 - 短信充值金额
    $(".sms-rating label:first").addClass("active");
    $(".sms-rating label").closest(".amounts").attr("fee", parseInt($(".sms-rating label:first").text()));
    $(".sms-rating label").on("click", function() {
        var amount = parseInt($(this).text());
        Cookies.set("amount-sms", amount);
    });
    var amountSms = Cookies.get("amount-sms");
    $(".sms-rating label").each(function() {
        if (parseInt($(this).text()) == amountSms) {
            $(".sms-rating label").removeClass("active");
            $(this).trigger("click");
        }
    });

    // 网银充值 - 提交按钮
    $("#btn-ebank").on("click", function() {
        var isValid = true;
        var panelWrap = $(this).closest(".pay-panel");
        var everyInput = panelWrap.find("input:not(.input-amount)");
        var errorInput = panelWrap.find(".has-error").find("input");
        var emptyFields = everyInput.filter(function() {
            return $.trim(this.value) === "";
        });
        everyInput.each(function() {
            $(this).trigger("blur");
        });
        if (panelWrap.find(".input-account").attr("isok") == 0) {
            isValid = false;
        }

        errorInput.each(function() {
            $(this).trigger("blur");
            if (emptyFields.length) {
                isValid = false;
            }
        });
        // 输入框有误
        if (errorInput.length) {
            isValid = false;
        }
        // 如果充值金额无效
        var amount = panelWrap.find(".input-amount-qrcode");
        var fee = amount.attr("fee");
        if (isNaN(fee) || fee <= 0) {
            amount.trigger("blur");
            isValid = false;
        }
        if (!isValid) {
            $(this).addTips();
        } else {
            $(this).removeTips();
            Cookies.set("amount-qrcode", fee);
            Cookies.set("pay-account", panelWrap.find(".input-account").val());
            // 提交数据
            if ((typeof subid !== "undefined") && subid == "zzb") {
                submitzzb();
            } else {
                submitbank();
            }
        }
    });

    // 实物卡|卡密充值 - 提交按钮
    $("#btn-trader").on("click", function() {
        var isValid = true;
        var panelWrap = $(this).closest(".pay-panel");
        var everyInput = panelWrap.find("input:not(.input-amount)");
        var errorInput = panelWrap.find(".has-error").find("input");
        var emptyFields = everyInput.filter(function() {
            return $.trim(this.value) === "";
        });
        everyInput.each(function() {
            $(this).trigger("blur");
        });
        errorInput.each(function() {
            $(this).trigger("blur");
            if (emptyFields.length) {
                isValid = false;
            }
        });
        // 输入框有误
        if (errorInput.length) {
            isValid = false;
        }
        if (!isValid) {
            $(this).addTips();
        } else {
            $(this).removeTips();
            // 提交数据
            submitecard();
        }
    });

    // 手机充值 - 提交按钮
    $("#btn-card, #btn-sms").on("click", function() {
        var isValid = true;
        var panelWrap = $(this).closest("ul");
        var everyInput = panelWrap.find("input:not(.input-amount)");
        var errorInput = panelWrap.find(".has-error").find("input");
        var emptyFields = everyInput.filter(function() {
            return $.trim(this.value) === "";
        });
        everyInput.each(function() {
            $(this).trigger("blur");
        });
        errorInput.each(function() {
            $(this).trigger("blur");
            if (emptyFields.length) {
                isValid = false;
            }
        });
        // 输入框有误
        if (errorInput.length) {
            isValid = false;
        }
        if (!isValid) {
            $(this).addTips();
        } else {
            $(this).removeTips();
            if ($(this).attr("id") == "btn-card") {
                submitphonecard();
            } else {
                submitphonesms();
            }
        }
    });

})